package com.tansuo365.test1.controller.othercontrol;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tansuo365.test1.bean.otherbean.ShengouDetails;
import com.tansuo365.test1.bean.otherbean.ShengouZong;
import com.tansuo365.test1.service.otherservice.ShengouDetailsService;
import com.tansuo365.test1.service.otherservice.ShengouZhixingService;
import com.tansuo365.test1.service.otherservice.ShengouZongService;
import com.tansuo365.test1.util.DateUtil;
import com.tansuo365.test1.util.StringUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@PropertySource(value="classpath:danhao.properties")
@Api(value = "申购单相关控制层",tags = "",description = "")
@RestController
@RequestMapping("/admin/shengoudan")
public class ShengouDanController {

    @Autowired
    private ShengouZongService shengouZongService;
    @Autowired
    private ShengouDetailsService shengouDetailsService;
    @Autowired
    private ShengouZhixingService shengouZhixingService;

    @Value("${danhao.shengoudan.prefix}")
    private String shengoudanPrefix;

    /**
     * 分页获取申购单  list
     * TODO 如果该申购单数据已经全部采购了或者有超量采购了,没有未采购,部分采购的情况 则返回 , 否则不予返回
     * @param shengouZong
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/getSelective")
    public Map<String,Object> getShengouDan(ShengouZong shengouZong, @RequestParam(value="page",required=true)Integer page, @RequestParam(value="rows",required=true)Integer rows){
        System.out.println("getSelective");
        if(shengouZong==null){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        PageHelper.startPage(page,rows);
        List<ShengouZong> shengouZongsList = shengouZongService.listShengouZong(shengouZong);
        PageInfo<ShengouZong> pageInfo = new PageInfo<ShengouZong>(shengouZongsList);
        resultMap.put("rows",pageInfo.getList());
        resultMap.put("total",pageInfo.getTotal());
        return resultMap;
    }

    /**
     * 获取申购单,申购单内数据子项为'未采购','部分采购'
     * @param shengouZong
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/getShengouDanNotFull")
    public Map<String,Object> getShengouDanNotFull(ShengouZong shengouZong,@RequestParam(value="page",required=true)Integer page, @RequestParam(value="rows",required=true)Integer rows){
        System.out.println("getSelective");
        if(shengouZong==null){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        PageHelper.startPage(page,rows);
        List<ShengouZong> shengouZongsList = shengouZongService.listShengouZongWhereNotFull(shengouZong);
        PageInfo<ShengouZong> pageInfo = new PageInfo<ShengouZong>(shengouZongsList);
        resultMap.put("rows",pageInfo.getList());
        resultMap.put("total",pageInfo.getTotal());
        return resultMap;
    }

    //到货单绑定申购单 回填数据

    @RequestMapping("/bangdingShengou")
    public Map<String,Object> getShengoudanById(ShengouZong shengouZong){
        Map<String,Object> resultMap = new HashMap<>();
//        List<ShengouZong> shengouZong1 = shengouZongService.listShengouZong(shengouZong);
        List<ShengouDetails> shengouDetailsList = shengouDetailsService.listShengouDetailsByZongId(shengouZong.getId());
//        resultMap.put("shengouZong",shengouZong1.get(0));
        resultMap.put("shengouDetailsList",shengouDetailsList);
        resultMap.put("success",true);
        return resultMap;
    }

//    public Map<String,Object> getShengouDanByArrId(@RequestParam("ids[]")Integer[] ids){
//        Map<String,Object> resultMap = new HashMap<>();
//    }

    /**
     * 根据zongzhang总表的id查询对应的子表数据 分页
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/listDetailsByZId")
    public Map<String,Object> listDetailsByZongId(ShengouDetails shengouDetails,@RequestParam(value="page",required=true)Integer page, @RequestParam(value="rows",required=true)Integer rows){
        if(shengouDetails==null){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        PageHelper.startPage(page,rows);
        List<ShengouDetails> shengouDetailsList = shengouDetailsService.listShengouDetailsSelective(shengouDetails);
        PageInfo<ShengouDetails> pageInfo = new PageInfo<ShengouDetails>(shengouDetailsList);
        resultMap.put("rows",pageInfo.getList());
        resultMap.put("total",pageInfo.getTotal());
        return resultMap;
    }

    //TODO 绑定申购单,数据根据实际数据来回填
    //返回的数据已经是只是未采购和部分采购
    @RequestMapping("/listDetailsByZIdBindCondition")
    public Map<String,Object> listDetailsByZongIdWhichNotFull(@RequestParam("id")Integer id){
        Map<String,Object> resultMap = new HashMap<>();
//        List<ShengouZong> shengouZong1 = shengouZongService.listShengouZong(shengouZong);
        List<ShengouDetails> shengouDetailsList = shengouDetailsService.listShengouDetailsWithDaohuoIfNotFull(id);
//        resultMap.put("shengouZong",shengouZong1.get(0));
        resultMap.put("shengouDetailsList",shengouDetailsList);
        resultMap.put("success",true);
        return resultMap;
    }

    /**
     * 获取申购单号 OK
     * @return
     * @throws Exception
     */
    @Async
    @RequestMapping("/getShengouNumber")
    public ListenableFuture<String> genBillCode()throws Exception{
        StringBuffer billCodeStr = new StringBuffer();
        billCodeStr.append(shengoudanPrefix);
        billCodeStr.append(DateUtil.getCurrentDateStr());
        String todayMaxShengouDanNumber = shengouZongService.getTodayMaxShengouDanNumber();
        if(todayMaxShengouDanNumber!=null){
            billCodeStr.append(StringUtil.formatCode(todayMaxShengouDanNumber));
        }else{
            billCodeStr.append("0001");
        }
        return new AsyncResult<>(billCodeStr.toString());
    }

    /**
     * 保存申购总表+子表数据 OK
     * @param shengouZong
     * @param wuliaoShengouDetailsJson
     * @return
     */
    @RequestMapping("/save")
    public Map<String,Object> save(HttpSession session, ShengouZong shengouZong, @RequestParam("wuliaoShengouDetailsJson") String wuliaoShengouDetailsJson){
        System.out.println("in shengoudan/save");
        System.out.println("shengouZong:"+shengouZong);
        System.out.println("shengouDetailsJson:"+wuliaoShengouDetailsJson);
        if(shengouZong==null || wuliaoShengouDetailsJson==null){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        Gson gson = new Gson();
        List<ShengouDetails> shengouDetailsList = gson.fromJson(wuliaoShengouDetailsJson, new TypeToken<List<ShengouDetails>>() {
        }.getType());
        shengouZong.setFounder((String) session.getAttribute("truename"));
        //save时,先保存大表,大表返回id后再保存附表
        int insertCode = shengouZongService.saveZongAndDetails(shengouZong, shengouDetailsList);
        //申购单保存时,要初始化到申购执行
        int i = shengouZhixingService.saveShengouToZhixing(shengouDetailsList, shengouZong);
        if(insertCode>0 && i>0){
            resultMap.put("success",true);
            resultMap.put("msg","保存成功!");
        }else {
            resultMap.put("success",false);
            resultMap.put("errorInfo","保存失败");
        }
        return resultMap;
    }

    /**
     * 更新申购总表
     * @param shengouZong
     * @return
     */
    @RequestMapping("/update")
    public Map<String,Object> update(ShengouZong shengouZong){
        if (shengouZong==null){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        int updateCode = shengouZongService.updateByPrimaryKeySelective(shengouZong);
        if(updateCode>0){
            resultMap.put("success",true);
        }else{
            resultMap.put("success",false);
        }
        return resultMap;
    }

//    /**
//     * 删除
//     * @param id
//     * @return
//     */
//    @RequestMapping("/delete")
//    public Map<String,Object> delete(Integer id){
//        if(id==null||id<0){
//            return null;
//        }
//        Map<String,Object> resultMap = new HashMap<>();
//        int zongDelCode = shengouZongService.deleteByPrimaryKey(id);
//        int ziDelCode = shengouDetailsService.deleteByZongId(id);
//        if(zongDelCode>0){
//            resultMap.put("success",true);
//        }else{
//            resultMap.put("success",false);
//        }
//        return resultMap;
//    }
//
    @RequestMapping("/delete")
    @Transactional
    public Map<String,Object> deleteBatch(@RequestParam("ids[]") Integer[] ids){
        Map<String,Object> resultMap = new HashMap<>();
        if(ids==null||ids.length==0){
            resultMap.put("success",false);
            resultMap.put("errorInfo","请选中条目进行删除");
            return resultMap;
        }
        List<String> shengoudans = null;
        shengoudans = shengouZongService.listShengouDanhao(ids);
        int code = shengouZongService.deleteBatchByPKArr(ids);
        int ziDelCode = 0;
        int deleteZhixingCode = 0;
        if(code>0){
            ziDelCode = shengouDetailsService.deleteByZongIdArr(ids);

        }
        if(ziDelCode>0){
            //删除申购执行中的申购数据
            deleteZhixingCode = shengouZhixingService.deleteByShengoudanhao(shengoudans);
        }
        if (code > 0 && ziDelCode>0 && deleteZhixingCode>0) {
            resultMap.put("success", true);
        } else {
            resultMap.put("success", false);
        }
        return resultMap;
    }
    
    
}
